// IPCfg.cpp : 实现文件
// implementation file

#include "stdafx.h"
#include "SingleDisplay.h"
#include "IPCfg.h"
#include "GenICam/GigE/GigECamera.h"
#include "GenICam/GigE/GigEInterface.h"
#include <sstream>
#include "resource.h"

// IPCfg 对话框
// IPCfg dialog

extern ::CString getParseLanguageString(::CString strOrl);

using namespace Dahua;
using namespace Dahua::GenICam;

IMPLEMENT_DYNAMIC(IPCfg, CDialog)

IPCfg::IPCfg(CWnd* pParent)
	: CDialog(IPCfg::IDD, pParent)
{

}

IPCfg::~IPCfg()
{
}

void IPCfg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

void IPCfg::init(Dahua::GenICam::ICameraPtr& camera, SetIPProc const& proc)
{
	{
		// 根据目前配置的语言初始化界面文本显示 | Initialize the text display of the interface according to the currently configured language

		this->SetWindowText(getParseLanguageString(_T("_CFGFORM_IP")));
		GetDlgItem(IDC_STATIC_ADDRESS)->SetWindowText(getParseLanguageString(_T("_CFGFORM_ADDRESS")));
		GetDlgItem(IDC_STATIC_SUBMASK)->SetWindowText(getParseLanguageString(_T("_CFGFORM_SUBNET")));
		GetDlgItem(IDC_STATIC_GATEWAY)->SetWindowText(getParseLanguageString(_T("_CFGFORM_GATEWAY"))); 
		GetDlgItem(IDC_BTN_SETIP)->SetWindowText(getParseLanguageString(_T("_CFGFORM_OK")));
	}


	assert(NULL != camera);
	
	_cameraPtr = camera;
	_setIpCallBack = proc;
	
	Dahua::GenICam::IGigECameraPtr cameraPtr = Dahua::GenICam::IGigECamera::getInstance(camera);
	Infra::CString ipAddr, mask, gateway;
	if (NULL == cameraPtr)
	{
		MessageBox("Get Camera Failed.", "", 0);
		return;
	}
	ipAddr = cameraPtr->getIpAddress();
	mask = cameraPtr->getSubnetMask();
	gateway = cameraPtr->getGateway();
	
	Dahua::GenICam::IGigEInterfacePtr interfacePtr = Dahua::GenICam::IGigEInterface::getInstance(camera);
	Infra::CString interfaceIpAddr, interfaceMask, interfaceGateway;
	if (NULL == interfacePtr)
	{
		MessageBox("Get Interface Failed.", "", 0);
		return;
	}
	interfaceIpAddr = interfacePtr->getIpAddress();
	interfaceMask = interfacePtr->getSubnetMask();
	interfaceGateway = interfacePtr->getGateway();
	
	Infra::CString disIPAddr;
	if (mask != interfaceMask || gateway != interfaceGateway)
	{
		// 获取IP地址的Vector
		// get vector of IP address
		std::string tempIp = interfaceIpAddr.c_str();
		std::replace(tempIp.begin(), tempIp.end(), '.', ' ');
		std::stringstream ipStrStream(tempIp);
		std::vector<std::string> ipVec;
		std::copy(std::istream_iterator<std::string>(ipStrStream),
			      std::istream_iterator<std::string>(),
				  std::back_inserter(ipVec));

		// 获取子网掩码的Vector
		// get vector of subnet address
		std::string tempSubnet = mask.c_str();
		std::replace(tempSubnet.begin(), tempSubnet.end(), '.', ' ');
		std::stringstream subNetStrStream(tempSubnet);
		std::vector<std::string> subNetVec;
		std::copy(std::istream_iterator<std::string>(subNetStrStream),
				  std::istream_iterator<std::string>(),
				  std::back_inserter(subNetVec));
		
		assert(4 == subNetVec.size() && 4 == ipVec.size());
		
		std::stringstream disStringStream; 
		for (int i = 0; i < 4; ++i)
		{
			int tempIp, tempSubnet;
			
			std::stringstream ss;
			ss << subNetVec[i];
			ss >> tempSubnet;
			
			std::stringstream ss1;
			ss1 << ipVec[i];
			ss1 >> tempIp;
			
			disStringStream << (tempIp & tempSubnet);
			if (i < 3)
			{
				disStringStream << ".";
			}
		}
		disIPAddr = disStringStream.str().c_str();
	}
	else
	{
		disIPAddr = ipAddr;
	}
	
	CIPAddressCtrl* pIPAddr = (CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS_IP);
	CIPAddressCtrl* pMask = (CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS_MASK);
	CIPAddressCtrl* pGateway = (CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS_GATEWAY);
	assert(pIPAddr && pMask && pGateway);
	
	fillAddress(disIPAddr, pIPAddr);
	fillAddress(interfaceMask, pMask);
	fillAddress(interfaceGateway, pGateway);
}

void IPCfg::fillAddress(Dahua::Infra::CString str, CIPAddressCtrl* ctrl)
{
	int szIp[4] = {0};
	int i = 0;
	
	std::string stdstrValue(str.c_str());
	while ( true )
	{
		std::string::size_type iPos = stdstrValue.find( "." );
		if ( iPos == std::string::npos )
		{
			std::stringstream ss;
			ss << stdstrValue;
			int val = 0;
			ss >> val;
			szIp[i++] = val;

			break;
		}
		
		std::string strSub = stdstrValue.substr( 0, iPos );
		stdstrValue = stdstrValue.substr( iPos+1 );
		std::stringstream ss;
		ss << strSub;
		int val = 0;
		ss >> val;
		szIp[i++] = val;
		
		iPos = stdstrValue.find( "." );
	}				
	ctrl->SetAddress(szIp[0], szIp[1], szIp[2], szIp[3]);
}

BEGIN_MESSAGE_MAP(IPCfg, CDialog)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BTN_SETIP, &IPCfg::OnBnClickedBtnSetip)
END_MESSAGE_MAP()


// IPCfg 消息处理程序
// IPCfg message processing 

void IPCfg::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	// add message processing code and / or call defaults here
	_cameraPtr.reset();
	
	CDialog::OnClose();
}

void IPCfg::OnBnClickedBtnSetip()
{
	::CString ip, subnet, gateway;
	GetDlgItem(IDC_IPADDRESS_IP)->GetWindowText(ip);
	GetDlgItem(IDC_IPADDRESS_MASK)->GetWindowText(subnet);
	GetDlgItem(IDC_IPADDRESS_GATEWAY)->GetWindowText(gateway);
	
	Dahua::GenICam::IGigECameraPtr cameraPtr = Dahua::GenICam::IGigECamera::getInstance(_cameraPtr);
	if (!cameraPtr->forceIpAddress(ip, subnet, gateway))
	{
		MessageBox("Set IP Failed.", "", 0);
	}
	else
	{
		_setIpCallBack();
		DestroyWindow();
	}
}
